package collections;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Map;
import java.util.Random;

import com.google.common.collect.Maps;
import org.junit.Test;

/**
 * @Author: ycy
 * @CreateDate: 2019/5/30 18:43
 */
public class MapListCompare {


  public static void main(String[] args) {
    System.out.println("start...");
    for (int i = 0; i < 10; i++) {
      new MapListCompare().test1("哈哈,haha,呵呵,hehe,嘿嘿,heihei,梦幻,menghuang,来了,laile,极限,jixian,可爱,keai");
    }


    System.out.println("start...");
    for (int i = 0; i < 10; i++) {
      new MapListCompare().test2("哈哈,haha,呵呵,hehe,嘿嘿,heihei,梦幻,menghuang,来了,laile,极限,jixian,可爱,keai");
    }



  }

  public void test1(String src) {
    List<String> list = Lists.newArrayList();
    String[] split = src.split(",");
    for (String s : split) {
      list.add(s);
    }
    Long startTime = System.currentTimeMillis();
    Random random = new Random();
    for (int i = 0; i < 10000000; i++) {
      int i1 = random.nextInt(split.length - 1);
      String s = split[i1];
      boolean contains = list.contains(s);
    }
    System.out.println(System.currentTimeMillis() - startTime);
  }

  /**
   *  使用map 要快
   * @param src
   */
  public void test2(String src) {
    Map<String, String> map = Maps.newHashMap();
    String[] split = src.split(",");
    for (String s : split) {
      map.put(s, s);
    }
    Long startTime = System.currentTimeMillis();
    Random random = new Random();
    for (int i = 0; i < 10000000; i++) {
      int i1 = random.nextInt(split.length - 1);
      String s = split[i1];
      boolean contains = map.containsKey(s);
    }
    System.out.println(System.currentTimeMillis() - startTime);
  }


}
